package cn.levi.sensitive.component;

import cn.levi.sensitive.configuration.SensitiveProperties;
import java.util.List;
import lombok.AllArgsConstructor;

/**
 * 替换掩码
 *
 * @author lei
 * @since 2025/2/20
 */
@AllArgsConstructor
public class ActionReplaceSensitiveWord implements ActionIfHasSensitiveWord{

	private SensitiveHandler sensitiveHandler;
	private SensitiveProperties sensitiveProperties;
	@Override
	public String doAction(String word) {
		String text="";
		List<String> allSensitive = sensitiveHandler.matchAllSensitive(word);
		String maskChar = sensitiveProperties.getMaskChar().toString();
		for (String sensitive : allSensitive) {
			StringBuilder targetMaskChar = new StringBuilder();
			int length = sensitive.length();
			for (int i = 0; i < length; i++) {
				targetMaskChar.append(maskChar);
			}
			text=word.replaceAll(sensitive, targetMaskChar.toString());
		}
		return text;
	}
}
